package com.coffee.takeoutserver.controller;

import com.coffee.takeoutserver.bean.MyCollection;
import com.coffee.takeoutserver.bean.Seller;
import com.coffee.takeoutserver.service.CollectionService;
import com.coffee.takeoutserver.service.SellerService;
import com.coffee.takeoutserver.util.RandomUtil;
import com.coffee.takeoutserver.util.RequestType;
import com.coffee.takeoutserver.util.ResponseInfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ImageFileController {

    @ResponseBody
    @RequestMapping("uploadImage")
    public String uploadImage(HttpServletRequest request,String type) throws IOException {
        Gson gson = new Gson();
        ResponseInfo responseInfo = new ResponseInfo();
        responseInfo.setCode("0");
        Map<String,String> map = new HashMap<>();
        map.put("requestType",RequestType.UploadImage);
        if (type == null || (!"GIF".equals(type.toUpperCase()) && !"PNG".equals(type.toUpperCase()) && !"JPG".equals(type.toUpperCase()))) {
            map.put("errorMsg","不支持该图片格式上传");
            responseInfo.setData(gson.toJson(map));
            return gson.toJson(responseInfo);
        }
        System.out.println("type:"+type);
        Part part = null;
        try {
            part = request.getPart("file");
        } catch (ServletException e) {
            e.printStackTrace();
        }
        InputStream inputStream = part.getInputStream();
        //项目在容器中实际发布运行的根路径
        File rootDir = new File("src/main/resources/static/image");
        if (!rootDir.exists()) {
            rootDir.mkdir();
        }
        String fileName = RandomUtil.usingUUID()+"."+type;
        String path = rootDir.getAbsolutePath() + File.separator + fileName;
        FileOutputStream outputStream = new FileOutputStream(new File(path));
        byte[] byt = new byte[1024];
        int len;
        while ((len = inputStream.read(byt)) != -1) {
            outputStream.write(byt, 0, len);
        }
        outputStream.close();
        inputStream.close();
        System.out.println("图片存储地址：" + path);
        map.put("errorMsg","");
        map.put("filePath","image/"+fileName);
        System.out.println("filePath:"+fileName);
        responseInfo.setData(gson.toJson(map));
        return gson.toJson(responseInfo);
    }
}
